﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoStuffStore.Common;

namespace InfoStuffStore
{
    public class AdDisplay
    {
        public string Title { get; set; }
        public string SiteLink { get; set; }
        public string ImageUrl { get; set; }
    }

    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetCategories();
                GetBrands();
                GetSideAds();
            }

            if (Request.Path.ToLowerInvariant() != "/default.aspx")
            {
                middle.Visible = false;
            }
        }

        private void GetCategories()
        {
            using (DBDataContext dataContext = new DBDataContext())
            {
                var result = dataContext.GetParentCategory();
                GridCategoryTopList.DataSource = result;
                GridCategoryTopList.DataBind();

                if (Request.QueryString.AllKeys.Contains("catid") && Request.QueryString["catid"] != "")
                {
                    var resultCat = dataContext.GetSubCategory(Convert.ToInt32(Request.QueryString["catid"]));
                    GridSubCatSide.DataSource = resultCat;
                    GridSubCatSide.DataBind();
                }
                else
                {
                    GridCatSide.DataSource = result;
                    GridCatSide.DataBind();
                }
            }
        }

        private void GetBrands()
        {
            using (DBDataContext dataContext = new DBDataContext())
            {
                var result = dataContext.GetRandomBrands();
                GridBrandList.DataSource = result;
                GridBrandList.DataBind();
            }
        }

        private void GetSideAds()
        {
            using (DBDataContext dataContext = new DBDataContext())
            {
                int count = 1;
                List<AdDisplay> leftAdDisplay = new List<AdDisplay>();
                List<AdDisplay> rightAdDisplay = new List<AdDisplay>();
                var result = dataContext.GetAdSpace();
                foreach (var item in result)
                {
                    leftAdDisplay.Add(new AdDisplay()
                    {
                        Title = item.Title,
                        ImageUrl = string.Format("Content/AdSpace/{0}", item.ImagePath),
                        SiteLink = item.SiteLink
                    });

                    count++;
                }

                grdAdvCampsRight.DataSource = leftAdDisplay;
                grdAdvCampsRight.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ProductList.aspx?search=" + txtSearch.Text);
        }
    }
}